<?php


class c__cache_line{

    public $timestamp;
    public $DATA;

    function __construct( $data ){
    
        if( isset($data) )
        {
            $this->DATA = $data;
            $this->timestamp = time();
        }else{
            
            return null;
        }
    }
    
    public function update( $data ){   $this->DATA = $data; }
}


class player_cache{

    private $lines;
    private $size;
    private $max_size;
    
    function __construct( $players = null, $maxsize = 32){
    
        $this->lines = array();
        $this->size = 0;
        $this->max_size = 32;
        
        if( isset($players) )
        {
            foreach( $players as $key => $player ){
                $this->lines[$key] = new c__cache_line( $player );
                $this->size++;
            }
        }
    }

    public function raise( $key, $data = null ){
        if( $this->size ){
            if( isset($data) ){ /* Trying to update a line */
                if( array_key_exists($key, $this->lines) ){
                    $this->lines[$key]->DATA = $data;
                    $this->lines[$key]->timestamp = time();
                    return;
                }else{
                    if( $this->size < $this->max_size ){
                        $this->lines[$key] = new c__cache_line( $data );
                        $this->size++;
                        return;
                    }else{
                        /* Cache has reached max size, so its time to dirty the oldest line */
                        $oldest_stamp = 0;
                        $oldest_line = null;
                        foreach( $this->lines as $index => $line ){
                            if( $oldest_stamp < $this->lines[$index]->timestamp ){
                                $oldest_stamp = $this->lines[$index]->timestamp;
                                $oldest_line = $index;
                            }
                        }
                        if( $oldest_line ){
                            $this->dirty( $oldest_line );
                            $this->lines[$key] = c__cache_line( $data );
                            $this->size++;
                            return;
                        }
                        return false; /* Assertion! should never reach this point */
                    }
                }
            }
            
            /* Trying to see if a line exists */
            if( array_key_exists($key, $this->lines) ){ /* 'HIT' */
                $this->lines[$key]->timestamp = time();
                return $this->lines[$key]->DATA;
            }else{
                /* $key can also be the ip */
                foreach( $this->lines as $index => $line )
                {
                    if( $index == $this->lines[$index]->$DATA['ip'] ){ /* 'HIT' */
                        $this->lines[$index]->timestamp = time();
                        return $this->lines[$index]->DATA;
                    }
                }
                /* MISS */
                return null;
            }
        }
    }
    
    private function dirty( $key ){
        
        $this->size--;
        unset($this->lines[$key]);
        ksort($this->lines);
    }
}
